<template>
  <van-popup v-model="show" :style="{ borderRadius: '8px' }">
    <div class="wrapper">
      <div class="flex">
        <img class="user" :src="user.avater" v-if="user.avater" />
        <div class="user" v-else>{{ user.userName | nameFormat }}</div>
        <div class="content">
          <div class="title-wrap">
            <div class="title">{{ user.userName }}</div>
            <div class="time">{{ appraisalData.year }}年第{{ appraisalData.quarter }}季度</div>
          </div>
          <div class="copy">考核单位：{{ appraisalData.deptNameTree }}</div>
          <div class="copy">隶属单位：{{ user.organizationName }}</div>
        </div>
      </div>
      <div class="select-list">
        <div class="list-item" v-for="(item, index) in statusList" :key="index">
          <div class="label">{{ item.description }}</div>
          <div v-for="(star, index) in item.starInfo" :key="index">
            <img @click="setGrade(item, index)" v-if="star.grade > item.grade" src="@/assets/normal/start.png" />
            <img @click="setGrade(item, index)" v-else src="@/assets/normal/start-active.png" />
          </div>
        </div>
      </div>
      <div class="btn-group">
        <div class="btn btn-active" @click="show = false">取消</div>
        <div class="btn btn-active" @click="onConfirm">确定</div>
      </div>
    </div>
  </van-popup>
</template>

<script>
import { appraisal } from "@/api/appraisalPeriod";
export default {
  props: {
    showPopup: {
      type: Boolean,
      default: false
    },
    user: {
      type: Object,
      default: () => {}
    },
    appraisalData: {
      type: Object,
      default: () => {}
    },
    multData: {
      type: Object,
      default: () => {}
    }
  },
  filters: {
    nameFormat(str) {
      return str ? str.slice(-2) : str;
    }
  },
  watch: {
    showPopup(val) {
      this.show = val;
    },
    show(val) {
      if (!val) {
        this.$emit("update:showPopup", false);
      } else {
        this.resetGrade();
      }
    },
    multData: {
      handler(val) {
        if (val.typeList && val.scoreList) {
          const starInfo = val.scoreList.reverse();
          this.statusList = val.typeList.map(type => {
            return {
              ...type,
              grade: 0,
              starCount: 0,
              starInfo: starInfo
            };
          });
        }
      },
      immediate: true
    }
  },
  data() {
    return {
      show: false,
      statusList: []
    };
  },
  methods: {
    resetGrade() {
      this.statusList.forEach(ele => (ele.grade = 0));
    },
    setGrade(item, index) {
      item.grade = item.starInfo[index].grade;
      item.starCount = index + 1;
    },
    onConfirm() {
      const param = {
        appraisalId: this.user.appraisalId,
        appraisalType: this.user.appraisalType,
        groupId: this.user.groupId,
        userId: this.user.id,
        typeRecordDetailList: this.statusList.map(s => {
          return {
            description: s.description,
            grade: s.grade,
            startCount: s.starCount,
            weight: s.weight
          };
        })
      };
      appraisal(param).then(res => {
        if (res.code == 200) {
          this.show = false;
          this.$emit("onSuccess");
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.wrapper {
  padding: 20px;
  width: 282px;
  box-sizing: border-box;
  .user {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    margin-right: 12px;
    background: #2492ff;
    color: #fff;
    text-align: center;
    line-height: 40px;
    font-size: 14px;
  }
  .content {
    flex: 1;
    line-height: 20px;
    .title-wrap {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 8px;
      .title {
        font-size: 14px;
        color: #333;
      }
      .time {
        color: #a3a7ad;
        font-size: 12px;
      }
    }
    .copy {
      font-size: 12px;
      color: #a3a7ad;
    }
  }
  .select-list {
    padding-left: 20px;
    .list-item {
      display: flex;
      align-items: center;
      margin-top: 16px;
      .label {
        font-size: 13px;
        color: #4d4d4d;
      }
      img {
        display: block;
        width: 16px;
        margin-left: 15px;
      }
    }
  }
  .btn-group {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 30px;
    padding: 0 18px;
    .btn {
      width: 92px;
      line-height: 32px;
      color: #fff;
      background: #2492ff;
      border-radius: 6px;
      text-align: center;
      &:first-child {
        background: #f5f5f5;
        color: #999999;
      }
    }
  }
}
</style>
